package com.wyl.salary.ui.page

import android.util.Log
import android.widget.Toast
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material.icons.filled.Done
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavHostController
import androidx.navigation.NavOptions
import com.wyl.salary.ui.components.AddSalary
import com.wyl.salary.ui.components.PreviewAddSalary
import com.wyl.salary.ui.theme.SalaryTheme
import com.wyl.salary.ui.viewmodel.AddSalaryViewModel

@Composable
private fun EditSalaryPage(
    onSaveSalary: () -> Unit,
    onDelSalary: () -> Unit,
    onBackPress: () -> Unit,
    content: @Composable () -> Unit,
) {
    val scrollState = rememberScrollState()
    val openDialog = remember { mutableStateOf(false) }
    Scaffold(
        modifier = Modifier.fillMaxSize(),
        topBar = {
            TopAppBar(
                title = { Text("查看记录") },
                navigationIcon = {
                    IconButton(onClick = onBackPress) {
                        Icon(imageVector = Icons.Filled.ArrowBack, contentDescription = "返回")
                    }
                },
                actions = {
                    IconButton(onClick = onSaveSalary) {
                        Icon(imageVector = Icons.Filled.Done, contentDescription = "完成")
                    }
                }
            )
        },
        content = {
            Column(
                modifier = Modifier
                    .fillMaxSize()
                    .verticalScroll(state = scrollState)
            ) {
                content()
                Button(
                    onClick = { openDialog.value = true },
//                    onClick = onDelSalary,
                    modifier = Modifier
                        .padding(16.dp)
                        .fillMaxWidth()
                ) {
                    Text(text = "删除")
                }
            }
            DelDialog(openDialog, onConfirm = onDelSalary)
        }
    )
}

@Composable
fun EditSalaryPageWithViewModel(
    navController: NavHostController,
    viewModel: AddSalaryViewModel = viewModel(key = "aDD"),
    sid: Long,
    onEditSalary: () -> Unit,
) {
    Log.d("TAGaaa", "EditSalaryPageWithViewModel: $viewModel")
    val context = LocalContext.current
    LaunchedEffect(sid) { viewModel.querySalary(sid) }
    EditSalaryPage(
        onSaveSalary = {
            viewModel.addSalary(onSuccess = {
                navController.popBackStack()
                onEditSalary()
            }) {
                Toast.makeText(context, it, Toast.LENGTH_SHORT).show()
            }
        },
        onDelSalary = {
            viewModel.delSalary()
//            navController.navigate(Router.EditSalaryPageDeleteDialog)
            navController.popBackStack()
            onEditSalary()
        },
        onBackPress = { navController.popBackStack() },
        content = {
            AddSalary(
                date = viewModel.date,
                hours = viewModel.hours,
                salary = viewModel.salary,
                tipping = viewModel.tipping,
                remarks = viewModel.remarks,
                onDateChange = { viewModel.date = it },
                onHoursChange = { viewModel.hours = it },
                onSalaryChange = { viewModel.salary = it },
                onTippingChange = { viewModel.tipping = it },
                onRemarksChange = { viewModel.remarks = it },
            )
        }
    )
}

@Composable
private fun DelDialog(openDialog: MutableState<Boolean>, onConfirm: () -> Unit) {
    if (openDialog.value) {
        AlertDialog(
            onDismissRequest = { openDialog.value = false },
            title = { Text(text = "警告") },
            text = {
                Text(
                    text = "确认删除该条记录吗？",
                )
            },
            confirmButton = {
                TextButton(onClick = {
                    openDialog.value = false
                    onConfirm()
                }) {
                    Text(text = "确认")
                }
            },
            dismissButton = {
                TextButton(onClick = { openDialog.value = false }) {
                    Text(text = "取消")
                }
            },
        )
    }
}

@Composable
fun DeleteDialog(
    navController: NavHostController,
    viewModel: AddSalaryViewModel = viewModel()
) {
    Log.d("TAGaaa", "DeleteDialog: $viewModel")
    AlertDialog(
        onDismissRequest = {
            navController.popBackStack()
        },
        title = { Text(text = "警告") },
        text = {
            Text(
                text = "确认删除该条记录吗？",
            )
        },
        confirmButton = {
            TextButton(onClick = {
                navController.popBackStack(Router.SalaryListPage,inclusive = true)
            }) {
                Text(text = "确认")
            }
        },
        dismissButton = {
            TextButton(onClick = { navController.popBackStack() }) {
                Text(text = "取消")
            }
        },
    )
}

@Preview
@Composable
private fun PreviewEditSalaryPage() {
    SalaryTheme {
        EditSalaryPage(
            onSaveSalary = {},
            onDelSalary = {},
            onBackPress = {},
            content = {
                PreviewAddSalary()
            }
        )
    }
}